<?php

namespace App\Http\Controllers\System;

use App\Game\LiveModel;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;


class LiveController extends Controller
{
    public static function _getPlayUrl($streamId,$bizId='19092')
    {
        return self::getPlayUrl($streamId,$bizId);
    }
    //普通直播(娃娃机摄像头)接入
    /**
     * 获取推流地址
     * 如果不传key和过期时间，将返回不含防盗链的url
     * @param bizId 您在腾讯云分配到的bizid
     *        streamId 您用来区别不同推流地址的唯一id
     *        key 安全密钥
     *        time 过期时间 sample 2016-11-12 12:00:00
     * @return String url */
    //echo getPushUrl("123456","8888","69e0daf7234b01f257a7adb9f807ae9f","2016-09-11 20:08:07");
    public function getPushUrl( $streamId,$bizId='19092',$key = null, $time = null)
    {
        //检测直播码是否存在我们数据库中
        $is_exists = DB::table('live')->where('live_code',$streamId)->select()->first();
        if (empty($is_exists))
        {
            return false;
        }
        if($key && $time){
            $txTime = strtoupper(base_convert(strtotime($time),10,16));
            //txSecret = MD5( KEY + livecode + txTime )
            //livecode = bizid+"_"+stream_id  如 8888_test123456
            $livecode = $bizId."_".$streamId; //直播码

            $txSecret = md5($key.$livecode.$txTime);

            $ext_str = "?".http_build_query(array(
                    "bizid"=> $bizId,
                    "txSecret"=> $txSecret,
                    "txTime"=> $txTime
                ));
        }
        return "rtmp://".$bizId.".livepush.myqcloud.com/live/".$livecode.(isset($ext_str) ? $ext_str : "");
    }


    /**
     * 获取播放地址
     * @param bizId 您在腾讯云分配到的bizid
     *        streamId 您用来区别不同推流地址的唯一id
     * @return String url */
    //print_r(getPlayUrl("123456","8888"));
    public function getPlayUrl($streamId,$bizId='19092')
    {
    	$streamIdArr = explode ('_',$streamId);
    	if (!empty($streamIdArr)&&$streamIdArr[0] == $bizId){
			$livecode 	= $streamId;
		}else{
			$livecode   = $bizId."_".$streamId; //直播码
		}

        $time       = time()+24*3600;
        $txTime     = strtoupper($time,10,16);
        $key        = '69e0daf7234b01f257a7adb9f807ae9f';
        $txSecret   = md5($key.$livecode.$txTime);

        $ext_str = "?".http_build_query(array(
                "bizid"=> $bizId,
                "txSecret"=> $txSecret,
                "txTime"=> $txTime
            ));


        return array(
            "rtmp://".$bizId.".liveplay.myqcloud.com/live/".$livecode.$ext_str,
            "http://".$bizId.".liveplay.myqcloud.com/live/".$livecode.".flv",
            "http://".$bizId.".liveplay.myqcloud.com/live/".$livecode.".m3u8"
        );
    }






}
